﻿Public Class SchedulerExt
    Implements IReloadCommand

    Public Property Pl() As PL.PLBase
    Public Property MainQueryName() As String
    Public FormName As String
    Public WithEvents Dataset As DataSet
    Public WithEvents MetaDataTable As CommonExt.MetaDataTable

    Public Event AfterLoad(ByVal sender As Object, ByVal e As System.EventArgs)


    Public Property CtrlName As String Implements IReloadCommand.CtrlName


    Public AllowCreate As Boolean
    Public AllowRead As Boolean
    Public AllowUpdate As Boolean
    Public AllowDelete As Boolean


    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        '
        AllowCreate = True
        AllowRead = True
        AllowUpdate = True
        AllowDelete = True
        '
        Dataset = New DataSet("GridExtPL")

    End Sub


    Private Sub InitGrid()
      
        '
        Try
            If Dataset IsNot Nothing AndAlso Dataset.Tables.Count > 0 Then
                MainQueryName = Dataset.Tables(0).TableName
                'Ici on se sert du parent de notre dataset
                'Par convention la table 0 de notre DataSet est le parent de nos relation
                MetaDataTable = CType(Dataset.Tables(0), MetaDataTable)
                SchedulerStorage1.Appointments.DataSource = Dataset.Tables(0)
                InitMainView()
                'Ici on parcours les tables a afficher

            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub


    Private Sub SchedulerExt_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        InitMainView()



        ' Later On for HAck 2
        If AfterLoadEvent IsNot Nothing Then
            RaiseEvent AfterLoad(sender, e)
        End If

    End Sub

    Private Sub InitMainView()
        Dim mainViewAccess As MetaQueryAccess = Nothing
        Dim aTable As MetaQuery
        Dim mqCtrlCode As String = Nothing
        Dim tableColumns As DataTable = Nothing


        SchedulerControl1.BeginUpdate()
        If Not String.IsNullOrWhiteSpace(FormName) Then
            'by design we restrict all access , BD
            Me.CtrlName = FormName
            Me.AllowRead = False
            Me.AllowUpdate = False
            Me.AllowDelete = False
            Me.AllowCreate = False
            If Pl IsNot Nothing Then
                'Testpoint: Here test if the Queryname match it's control Name 'Case sensitive'
                mqCtrlCode = AppManager.GetControlMqFromForm(FormName, Pl.QueryName)
                If Not String.IsNullOrWhiteSpace(mqCtrlCode) Then
                    mainViewAccess = New MetaQueryAccess(mqCtrlCode)
                    Me.AllowRead = CBool(mainViewAccess.AllowRead)
                    Me.AllowUpdate = mainViewAccess.AllowUpdate
                    Me.AllowDelete = mainViewAccess.AllowDelete
                    Me.AllowCreate = mainViewAccess.AllowCreate
                End If
            End If
        End If
        If AllowRead Then
            If Pl IsNot Nothing Then
                Pl.InitData(False)
                Me.Dataset = Pl.Dataset
            End If
        End If


        If Dataset IsNot Nothing AndAlso Dataset.Tables.Count > 0 Then
            MainQueryName = Dataset.Tables(0).TableName
            'Ici on se sert du parent de notre dataset
            'Par convention la table 0 de notre DataSet est le parent de nos relation
            MetaDataTable = CType(Dataset.Tables(0), MetaDataTable)
            SchedulerStorage1.Appointments.DataSource = Dataset.Tables(0)

            '
            tableColumns = CommonExt.Base.AppManager.GetUserColumnSecurity(MetaDataTable.TableName, CommonExt.Base.AppManager.UserID)
            '
            Me.MetaDataTable.DefaultView.AllowEdit = Me.AllowUpdate
            Me.MetaDataTable.DefaultView.AllowDelete = Me.AllowDelete
            Me.MetaDataTable.DefaultView.AllowNew = Me.AllowCreate

            Me.SchedulerStorage1.Appointments.Labels.Clear()
            aTable = MetaQuery.GetMetaQueryByName("AppointmentLabel")

            aTable.FillResultQuery()
            For Each row As DataRow In aTable.ResultQuery.Rows
                Me.SchedulerStorage1.Appointments.Labels.Add(SetColor(row("color")), row("Text").ToString, row("MenuCaption").ToString)
            Next

            Me.SchedulerStorage1.Appointments.Mappings.AllDay = "AllDayEvent"
            Me.SchedulerStorage1.Appointments.Mappings.Description = "Body"
            Me.SchedulerStorage1.Appointments.Mappings.End = "End"
            Me.SchedulerStorage1.Appointments.Mappings.Label = "Label"
            Me.SchedulerStorage1.Appointments.Mappings.Location = "Location"
            Me.SchedulerStorage1.Appointments.Mappings.Start = "Start"
            '  Me.SchedulerStorage1.Appointments.Mappings.Status = "Status"
            Me.SchedulerStorage1.Appointments.Mappings.Subject = "Subject"
            Me.SchedulerStorage1.Appointments.Mappings.Type = "Type"
        End If
        If tableColumns IsNot Nothing Then
            tableColumns.Dispose()
        End If
        SchedulerControl1.EndUpdate()
    End Sub
    Public Shared Function SetColor(ByVal afield As Object) As System.Drawing.Color
        If afield IsNot Nothing AndAlso Not IsDBNull(afield) Then
            If CType(afield, Decimal) = 0 Then
                Return System.Drawing.Color.FromArgb(CInt(afield))
            Else
                Return System.Drawing.Color.FromArgb(CInt(afield))
            End If

        End If
    End Function






    Public Sub ReloadCommand() Implements CommonExt.IReloadCommand.ReloadCommand

    End Sub
End Class
